package com.F03.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F03.actionform.F0303ActionForm;
import com.F03.dao.PdPlanningDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.info.T_S_YHCZXXinfo;
import com.util.ExcelExport;
public class F0303Action extends BaseAction {

	/**
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	Logger log = Logger.getLogger(F0303Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		F0303ActionForm actionForm = (F0303ActionForm)form;
		PdPlanningDAO dao = new PdPlanningDAO();
		String operationType = actionForm.getOperationType();
		String strList = "list";
		
		try {
			//操作记录
			t_YhczxxInfo = setYhczxx(request, response, operationType,"F0303");

			/*
			if(Constants.BACK.equals(operationType)){
				return mapping.findForward(strList);
			}
			*/

			if(Constants.QUERYLIST.equals(operationType)){
				doQueryList(actionForm,dao.queryPdPlanningReportList(actionForm));
				//actionForm.setTotalList(dao.queryTotalList(actionForm));
				return mapping.findForward(strList);
			}
			else if(Constants.INIT.equals(operationType)){
				return mapping.findForward(strList);
			}
			else if("export".equals(operationType)){
				doExport(actionForm, dao, response);
				return mapping.findForward("list");
			}
			
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}
		return mapping.findForward(strList);
	}
	
	private void doQueryList(F0303ActionForm actionForm,List list){
		if(list!=null&&list.size()>0){
			actionForm.setList(list);
		}
		else{
			actionForm.setList(list);
			actionForm.setErrorFlag("2");
		}	
	}
	
	private void doExport(F0303ActionForm actionForm, PdPlanningDAO dao, HttpServletResponse response){
		List titleList = new ArrayList();
		try {
			String fileName = "生产计划报表";
			/* 标题设定 */
			titleList.add("工作号");
			titleList.add("产品模型");
			titleList.add("总数量");
			titleList.add("组");
			titleList.add("数量");
			titleList.add("当前流程");
			titleList.add("开始时间");
			titleList.add("结束时间");
			/* sql作成 */
			String sql = getSql(actionForm);

			/* excel输出 */
			ExcelExport.export(fileName, titleList, sql, response);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
	}
	
	public String getSql(F0303ActionForm actionForm){
		StringBuffer sql=new StringBuffer();
		sql.append(" select h.manufacturing_order,h.model,h.quantity,h.groups,h.group_qty,");
    	//sql.append(" j.phases,j.begin_date,j.end_date from "); 
		sql.append(" j.phases,Convert(varchar(10),j.begin_date,120) begin_date,Convert(varchar(10),j.end_date,120) end_date from ");
    	sql.append(" (select e.manufacturing_order,e.groups,e.group_qty,e.quantity,e.family_code,");  
    	sql.append(" d.model "); 
    	sql.append(" from pd_planning e,c_orders d "); 
		sql.append(" where e.manufacturing_order = d.manufacturing_order) h, "); 
		
		sql.append(" (select a.family_code,b.phases,a.serial,c.groups,");  
		sql.append("  adyh.FUN_GETGROUPBEGINDATE(a.family_code,a.serial,c.finish_date) as begin_date,"); 
		sql.append(" adyh.FUN_GETGROUPENDDATE(a.family_code,a.serial,c.finish_date) as end_date"); 
		sql.append(" from t_family a,t_critical_phases b,pd_planning c"); 
		sql.append(" where c.family_code = a.family_code and ");  
		sql.append(" a.phases_code = b.code"); 
		sql.append(" group by a.family_code,a.serial,b.phases,c.finish_date,c.groups");  
		sql.append(" ) j"); 
		sql.append(" where h.family_code = j.family_code and h.groups = j.groups "); 
		
		if(!actionForm.getManufacturingOrder1().equals("")){
    		sql.append(" and h.manufacturing_order >='"+actionForm.getManufacturingOrder1()+"' ");
    	}
    	if(!actionForm.getManufacturingOrder2().equals("")){
    		 sql.append(" and h.manufacturing_order <='"+actionForm.getManufacturingOrder2()+"' ");
    	}
    	if(!actionForm.getStartDate1().equals("")){
    		sql.append(" and j.begin_date >= '"+actionForm.getStartDate1()+"'");
    	}
    	if(!actionForm.getStartDate2().equals("")){
    		sql.append(" and j.begin_date <= '"+actionForm.getStartDate2()+"'");
    	}
    	if(!actionForm.getFinishDate1().equals("")){
    		sql.append(" and j.end_date >= '"+actionForm.getFinishDate1()+"'");
    	}
    	if(!actionForm.getFinishDate2().equals("")){
    		sql.append(" and j.end_date <= '"+actionForm.getFinishDate2()+"'");
    	}
		sql.append(" order by h.family_code,h.groups,j.serial");
		return sql.toString();
	}
}